package com.example.game_forum.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.game_forum.dto.TCommentContentDto;
import com.example.game_forum.dto.TCommentDto;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TCommentRepository extends BaseMapper<TCommentDto> {

    @Select("select c.comment_id, c.post_id, c.user_id, c.content, c.parent_id, " +
            "c.like_count, c.create_time, c.update_time, " +
            "u.username, u.avatar, u.gender, u.level " +
            "from t_comment c " +
            "left join t_user u on c.user_id = u.user_id " +
            "where c.post_id = #{postId}")
    @Results({
            @Result(property = "commentId", column = "comment_id"),
            @Result(property = "postId", column = "post_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "content", column = "content"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "likeCount", column = "like_count"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "userName", column = "username"),
            @Result(property = "userAvatar", column = "avatar"),
            @Result(property = "userGender", column = "gender"),
            @Result(property = "userLevel", column = "level")
    })
    List<TCommentContentDto> selectCommentsByPostId(@Param("postId") String postId);

    @Insert("insert into t_comment (post_id, user_id, content) " +
            "values (#{postId}, #{userId}, #{content}")
    @Options(useGeneratedKeys = true, keyProperty = "commentId")
    void addComment(TCommentDto commentDto);

    @Insert("insert into t_comment (post_id, user_id, content, parent_id) " +
            "values (#{postId}, #{userId}, #{content}, #{parentId})")
    @Options(useGeneratedKeys = true, keyProperty = "commentId")
    void replyComment(TCommentDto commentDto,String parentId);

    @Update("update t_comment set " +
            "content = #{comment}, " +
            "update_time = CURRENT_TIMESTAMP " +
            "where comment_id = #{commentId}")
    void updateComment(@Param("commentId") String commentId, @Param("comment") String comment);

    @Delete("delete from t_comment where comment_id = #{commentId}")
    void deleteComment(@Param("commentId") String commentId);
} 